function createTable(ID,y,m)
{
        jQuery("#gbox_table"+ID).replaceWith($('<div id="pager'+ID+'"></div><table id="table'+ID+'"></table>'));
        var lastsel2=0;
        var lastsel=0;
        var days=(m!=2?((m%2)^(m>7))+30:(!(y%400)||!(y%4)&&(y%25)?29:28))
        var names=['ID',name];
        var cols=[{name:'id',index:'id',key:true, width:60,sortable: false, align:"center", hidden:true},
                          {name:'name',index:'name', width:200,sortable: false,editable:false}];
        for(var i=1;i<=days;i++)

        {

                        names[i+1]=""+i+"<br>"+dayA[new Date(y,m-1,i).getDay()];
                        cols[i+1]={name:'d'+i,index:'d'+i, width:40,editable:true,sortable: false,editrules:{required:false,integer:true}}
                }
        jQuery("#table"+ID).jqGrid({
                url:'/admin/showprice/'+y+'/'+m+'/'+ID,
            editurl:'/admin/editprice/'+y+'/'+m+'/'+ID,
                datatype: "json",
            mtype: 'POST',
                colNames:names,
                colModel:cols,
                pager: '#pager'+ID,
            viewrecords: true,
            caption:"&nbsp;",
            
            width:"1200",
            height:"100%"
        });
        jQuery("#table"+ID).jqGrid('navGrid','#pager'+ID,{edit:true,add:false,del:false,search:false});
}

function createUserTable(idserv,y,m)
{
   		jQuery("#gbox_tableUserPrice").replaceWith($('<div id="pagerUserPrice"></div><table id="tableUserPrice"></table>'));
        var lastsel2=0;
        var lastsel=0;
        var days=(m!=2?((m%2)^(m>7))+30:(!(y%400)||!(y%4)&&(y%25)?29:28))
        var names=['ID',name];
        var cols=[{name:'id',index:'id',key:true, width:1,sortable: false, align:"center", hidden:true},
                          {name:'name',index:'name', width:200,sortable: false,editable:false}];
        for(var i=1;i<=days;i++)
        {
                names[i+1]=""+i+"<br>"+dayA[new Date(y,m-1,i).getDay()];
                cols[i+1]={name:'d'+i,index:'d'+i, width:40,editable:true,sortable: false}
        }
        jQuery("#tableUserPrice").jqGrid({
                url:'/admin/showUserPrice/'+y+'/'+m+'/'+idserv,
                editurl:'/admin/editUserPrice/'+y+'/'+m+'/'+idserv,
                datatype: "json",
            mtype: 'POST',
                colNames:names,
                colModel:cols,
                pager: '#pagerUserPrice',
            viewrecords: true,
            caption:'AgentPrice',
             width:"1200",
            height:"100%"
        });
        jQuery("#tableUserPrice").jqGrid('navGrid','#pagerUserPrice',{edit:true,add:false,del:false,search:false});
        
        $("#gbox_tableUserPrice span.ui-jqgrid-title").replaceWith("<div class='editPriceTable' idTable="+idserv+"><a href='javascript:void(0)' >Редактировать данные</a></div>");
        $(".editPriceTable").contextMenu({ menu: 'myMenu', leftButton: true },
                        function(action, el, pos) { contextMenuWork(action, el, pos); });
}

function rows2()
        {
        	mRows='';
        	$('#tableUserPrice').find('tr.jqgrow').each(function()
        		{
        			mRows+='<tr><td>'+$(this).find('td').eq(1).html()+'</td><td><input type="text" name="r['+$(this).find('td').eq(0).html()+']"></td></tr>'
        		}
        	);
        	$('#addPriceTable').html(mRows);
        }
   function contextMenuWork(action, el, pos)
   {
   		switch (action)
        {
            case "delete":
	        	
                $('.mWindowTestDelete')
				.css('left',($(window).width()/2+$(window).scrollLeft()-200)+'px')
				.css('top',($(window).height()/2+$(window).scrollTop()-200)+'px')
                .toggle();
            break;
            case "deletebymonth":
                $('#hiddenf3').val($(el).attr('idtable'));
                $('.mWindowTestDeleteByMonth')
                .css('left',($(window).width()/2+$(window).scrollLeft()-200)+'px')
				.css('top',($(window).height()/2+$(window).scrollTop()-200)+'px')
                .toggle();
            break;
            case "addprice":
                $('#hiddenf1').val($('#service').attr('service'));
                rows2();
                $('#Calendar')
				.css('left',($(window).width()/2+$(window).scrollLeft()-300)+'px')
				.css('top',($(window).height()/2+$(window).scrollTop()-200)+'px');
                $('.editPrice')
                .css('left',($(window).width()/2+$(window).scrollLeft()-200)+'px')
				.css('top',($(window).height()/2+$(window).scrollTop()-200)+'px')
                .toggle();
            break;
        }
    }
    $(function(){
    	$('.editPrice').mwindow({
                trigger:'class',
                context:'<table><tr><td><div class="navLeft">Cena</div></td><td><div class="navRight">Skidka</div></td></tr></table><input type="hidden" id="hiddenf1" name="idServ" value=""><input type="hidden" name="Skidka" id="skidka" value=0><table id="addPriceTable"></table><table><tr><td>Dates</td><td><input type=text id=dates name=dates><input type=button value=".." class="Calendar"></td></tr></table>',
                url:'/admin/addUserPrices/',
                submit:true,
                name:'Ввод данных',
                success:function(e)
                        {
                                $('#tableUserPrice').trigger('reloadGrid');
                                //$('#tableUserPrice').trigger('reloadGrid');
                                
                        }
        });
          $('.mWindowTestDeleteByMonth').mwindow({
                trigger:'deleteTableDataByMonth',
                context:delete_conf+'<br/>',
                url:'/admin/deleteUserTableDataByMonth/'+$('#service').attr('service')+'/'+$('.goMonthChecked').attr('month')+'/'+$('.goMonthChecked').attr('year'),
                submit:true,
                width:220,
                height:150,
                success:function(e){
                        $('#tableUserPrice').trigger('reloadGrid');
                }
        });
        $('.mWindowTestDelete').mwindow({
                trigger:'deleteTableData',
                context:delete_conf+'<br/>',
                url:'/admin/deleteUserTableData/'+$('#service').attr('service'),
                submit:true,
                width:220,
                height:150,
                success:function(e){
                        $('#tableUserPrice').trigger('reloadGrid');
                        //console.log('#table'+$('#hiddenf2').val()+" "+$('#addTable').attr('service'));
                }
        });
          // go to next month
         $('.goMonth:eq(0)').addClass('goMonthChecked');
        $('.goMonth').click(function()
        {
                $('.goMonth').each(function(){
                        if($(this).hasClass('goMonthChecked'))$(this).removeClass('goMonthChecked');
                });
                $(this).addClass('goMonthChecked');
                createUserTable($('#service').attr('service'),$(this).attr('year'),$(this).attr('month'));
                for(var i=0;i<tables.length;i++)
                {
                        createTable(tables[i],$(this).attr('year'),$(this).attr('month'));
                }
        }); 
        $('.navLeft').click(function(){
        	$('#skidka').val('0');
        	$(this).addClass('goMonthChecked');
        	$('.navRight').removeClass('goMonthChecked');
        });
         $('.navRight').click(function(){
        	$('#skidka').val('1');
        	$(this).addClass('goMonthChecked');
        	$('.navLeft').removeClass('goMonthChecked');
        });
        // modal window whith calendar
        $('#Calendar').mcalendar({
                trigger:'Calendar',
                onSubmit:function(data)
                {
                        $('#dates').val(data);
                }
        });
    });